﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MixScript.BLL.FI
{
    public class cAccLedger : cBaseBLL
    {


        /// <summary>
        /// cAccLedger
        /// </summary>
        /// <param name="acc"></param>
        /// <param name="period"></param>
        /// <param name="process"></param>
        /// <param name="dbcd"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool getLedger(ref DataTable dt)
        {
            try
            {
                dt = new DataTable();
                string sql = @"SELECT a.acctnum idcoa, a.acctname coaname,a.process,a.dbcd as coatype,
                        CASE a.process WHEN 0 THEN 'None' WHEN 1 THEN 'Cash' WHEN 2 THEN 'Bank' WHEN 3 THEN 'A/R Customer' WHEN 4 THEN 'A/R Credit Card' WHEN
                        5 THEN 'A/R Employee' WHEN 6 THEN 'A/P Supplier' WHEN 7. THEN 'Vehicle' WHEN 8 THEN 'Raw Material' WHEN 9 THEN 'Sub Material' WHEN 10 THEN
                        'Work In Process' WHEN 11 THEN 'Finished Goods' END AS Process_name
                        ,CASE c.saldodbcd WHEN 'D' THEN valconvert end AS DEBIT ,
                        CASE c.saldodbcd when 'C' THEN valconvert end AS CREDIT
                        FROM  accaccounts a  
                        LEFT JOIN accledger c ON a.acctnum = c.acctnum 
                        WHERE a.aplication IN ('1','2','3') ORDER BY a.acctnum";
                dt = Dbserver.ExecuteQuery(sql, "vw_coamst");

                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            { throw ex; }


        }

        /// <summary>
        /// cAccLedger
        /// </summary>
        /// <param name="acc"></param>
        /// <param name="period"></param>
        /// <param name="process"></param>
        /// <param name="dbcd"></param>
        /// <param name="value"></param>
        /// <returns></returns>

        public bool SaveLedger(string acc, string period, string process, string dbcd, string value)
        {
            string sSql = "DELETE FROM accledger WHERE acctnum= '{0}'";
            try
            {
                Dbserver.ExecuteNonQuery(String.Format(sSql, acc));
            }
            catch (Exception ex)
            { throw ex; }

            sSql = @"INSERT INTO accledger (kd_cmpy,acctnum,period_id,process,saldodbcd,valconvert) 
            VALUES ('1','{0}','{1}','{2}','{3}','{4}')";
            try
            {
                Dbserver.ExecuteNonQuery(String.Format(sSql, acc, period, process, dbcd, value));
                return true;
            }
            catch (Exception ex)
            { throw ex; }

        }
    }
}
